package com.siyu.preplan.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.preplan.planpdocument.domain.PlanPDocument;
import com.siyu.preplan.planpdocument.service.IPlanPDocumentService;
import com.siyu.preplan.service.PlanPService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.preplan.dto.PlanListDto;
import com.siyu.preplan.service.impl.PlanPServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


@RestController
@RequestMapping("/preplan/manage")
public class PlanPController{

    // 假设文件存储在本地的特定目录下，这里定义目录路径
    private static final Path DOWNLOAD_DIR = Paths.get("your/download/directory");

    @Autowired
    private PlanPService planPService;

    @Autowired
    private IPlanPDocumentService planPDocumentService;

    /**
     * 预案列表查询
     * @param planListDto
     * @return
     */
    @PostMapping("/plan/list")
    public AjaxResult getPlanList(@RequestBody PlanListDto planListDto){
        Object hashMap = planPService.getPlanList(planListDto);
        return AjaxResult.success(hashMap);
    };

    /**
     * 上传文档
     * @param file
     * @param year
     * @param area
     * @return
     */
    @PostMapping(value = "/plan/upload", consumes = "multipart/form-data")
    public ResponseEntity<AjaxResult> uploadPlan(@RequestParam("file") MultipartFile file,
                                                 @RequestParam("year")String year,
                                                 @RequestParam("area")String area,
                                                 @RequestParam("rscd")String rscd) {
        return planPService.uploadPlan(file,year,area,rscd);
    }

    /**
     * 下载文件
     * @param id 文件名
     * @return 文件资源作为HTTP响应
     */
    @GetMapping("/plan/download")
    public ResponseEntity<Resource> downloadFile(@RequestParam("id") String id) throws Exception {
        return planPService.downloadFile(id);
    }


    /**
     * 删除文件
     * @param id
     * @return
     */
    @DeleteMapping("plan/deletefile")
    public AjaxResult deletefile(@RequestParam("id") String id){
        return planPService.deletefile(id);
    }

    /**
     * 文档展示
     * @param id
     * @return
     */
    @GetMapping("/plan/reveal")
    public AjaxResult revealById(@RequestParam("id") String id) {
        QueryWrapper<PlanPDocument> planPDocumentQueryWrapper = new QueryWrapper<>();
        planPDocumentQueryWrapper.select("plan_url").eq("id", id);
        PlanPDocument planPDocument = planPDocumentService.getOne(planPDocumentQueryWrapper);
        return AjaxResult.success(planPDocument);
    }



}
